package com.istock.servicesample.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.sampleService.api.SenvonInfoApi;
import com.istock.sampleService.model.SenvonInfoShowListRequest;
import com.istock.sampleService.model.SenvonInfoVO;
import com.istock.sampleservice.api.ShowHeaderApi;
import com.istock.sampleservice.service.SenvonInfoService;
import com.istock.servicesample.model.SenvonInfo;

@RequestMapping("senvonInfo")
@RestController
public class SenvonInfoController implements SenvonInfoApi{

	@Autowired
	private SenvonInfoService service;
	
	@Autowired
	private ShowHeaderApi showHeaderClient;
	
	@RequestMapping(value="showList",method=RequestMethod.POST)
	public ApiBaseResponse<PageInfo<SenvonInfoVO>> showList(@Validated @RequestBody SenvonInfoShowListRequest request){
		PageInfo<SenvonInfoVO> pageInfo = new PageInfo<SenvonInfoVO>();
		if(request.getPageNo()!=null && request.getPageNo() >0) {
			pageInfo.setCurrent(request.getPageNo());
		}
		if(request.getPageSize() != null && request.getPageSize() >0) {
			pageInfo.setLimit(request.getPageSize());
		}
		service.findByName(request.getName() , pageInfo);
		
		ApiBaseResponse<String> result = showHeaderClient.showHeader();
		
		return new ApiBaseResponse<PageInfo<SenvonInfoVO>>(RetStatusEnum.SUCCESS , "S", "查询成功" , pageInfo);
	}
	
	/**
	 * controller的方法入参
	 * 
	 * 不能写成  @RequestBody @NotBlank(message="名称不能为空")String name
	 * requestBody在解析string的时候,会把原始body变成string,传入方法
	 * 
	 * 也不能写成
	 * @RequestBody ApiBaseRequest<String> request
	 * 这种方式,validate注解无效,校验规则无法传入
	 * 
	 * {
		  "param":{"name":"senvon112"},"xxName":"123"
		}
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ApiBaseResponse<Void> add(@Validated @RequestBody SenvonInfoShowListRequest request){
		service.createRecord(request.getName());
		return new ApiBaseResponse<Void>(RetStatusEnum.SUCCESS , "S", "新增成功" , null);
	}
	
	@RequestMapping(value="exception",method=RequestMethod.POST)
	public ApiBaseResponse<Void> exception(@RequestBody String name){
		throw new RuntimeException("i have exception");
	}
}
